package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
import cn.com.hfuu.module.operate;
import cn.com.hfuu.module.user;
import cn.com.hfuu.module.warehouseteax;
import cn.com.hfuu.module.warehouseteaxlog;
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.OperateService;
import cn.com.hfuu.service.TeaxService;
import cn.com.hfuu.service.TeaxlogService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;

/**
 * 鲜叶仓库日志记录
 * @author git
 *
 */
@Controller
@RequestMapping("teaxlog")
public class TeaxlogControl { 

	@Autowired
	TeaxlogService teaxlogservice;
	
	@Autowired
	CodeService codeservice;
	
	@Autowired
	TeaxService teaxservice;
	@Autowired
	UserService userService;
	 
	@Autowired
	OperateService operateservice;
	private List<code> codelist;//出入库
	
	@RequestMapping("info")
	public String info(Model m){
		codelist=codeservice.selectbyType("07");
		m.addAttribute("codelist", codelist);
		return "warehouse/teaxlog/teaxlog";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, warehouseteaxlog teaxlog){
		System.out.println(teaxlog.getWarehouseTeaxlogid());
		List<warehouseteaxlog> teaxlist=teaxlogservice.selectPage(start,limit,teaxlog);
		List<warehouseteaxlog> total=teaxlogservice.selectpagetotal(teaxlog);
		List<warehouseteax> teaxl=teaxservice.selectall();
		List<user> userlist = userService.selectall();
		for (warehouseteaxlog teaxlog1 : teaxlist) {
			for (code c : codelist) {
				if(teaxlog1.getWarehouseTeaxtype()==c.getCodeValue())
					teaxlog1.setCode(c);
			}
			for (warehouseteax t : teaxl) {
				if(t.getWarehouseTeaxid().equals(teaxlog1.getWarehouseTeaxid()))
					teaxlog1.setWarehouseteax(t);
			}
			for (user user : userlist) {
				if(user.getUserId().equals(teaxlog1.getWarehouseUserid()))
					teaxlog1.setUser(user);
			}
			System.out.println(teaxlog1.toString());
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(teaxlist );
		page.setTotal(total.size() );
		return page;
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
		List<warehouseteax> teaxl=teaxservice.selectall();
		List<user> userlist = userService.selectall();
		warehouseteaxlog tl=new warehouseteaxlog();
		tl.setWarehouseTeaxtype(-1);
 		List<warehouseteaxlog> list = teaxlogservice.selectpagetotal(tl);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (warehouseteaxlog teax1 : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			 
			for (code c : codelist) {
				if(teax1.getWarehouseTeaxtype()==c.getCodeValue())
					teax1.setCode(c);
			}
			for (warehouseteax t : teaxl) {
				if(t.getWarehouseTeaxid().equals(teax1.getWarehouseTeaxid()))
					teax1.setWarehouseteax(t);
			}
			for (user user : userlist) {
				if(user.getUserId().equals(teax1.getWarehouseUserid()))
					teax1.setUser(user);
			}
			
			map1.put("编号", teax1.getWarehouseTeaxlogid());
			map1.put("操作",teax1.getCode().getCodeName());
			map1.put("茶名",teax1.getWarehouseteax().getWarehouseTeaname());
			map1.put("重量(克)",""+teax1.getWarehouseTeaxquality());
			map1.put("时间",teax1.getWarehouseTeaxlogtime());
			map1.put("负责人",teax1.getUser().getUserName());
			mapList.add(map1);

		}
		String[] title = { "编号", "操作","茶名", "重量(克)","时间","时间", };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "鲜叶库存日志信息");
		return new ModelAndView(ve, map);
	}
	
	private warehouseteaxlog teaxlog1;
	@RequestMapping("outlogedit")
	public ModelAndView edit(String id){
		teaxlog1=teaxlogservice.selectbyid(id);
		List<warehouseteax> teaxlist=teaxservice.selectall();
		 ModelAndView mv=new ModelAndView();
		 mv.setViewName("warehouse/teaxlog/editout");
		 mv.addObject("teaxlog", teaxlog1);
		 mv.addObject("teax", teaxlist);
		return mv;
		
	}
	
	@RequestMapping("outproc")
	@ResponseBody
	public String editproc(warehouseteaxlog teaxlog){
		warehouseteax oldt=teaxservice.selectbyid(teaxlog1.getWarehouseTeaxid());//数据库中目前的信息
		if(teaxlog1.getWarehouseTeaxid().equals(teaxlog.getWarehouseTeaxid())){//出库原料未改变，改变了数量
			float total=oldt.getWarehouseTeaquality();
			total+=teaxlog1.getWarehouseTeaxquality();
			System.out.println(teaxlog.toString());
			total-=teaxlog.getWarehouseTeaxquality();
			if(total<0){
				return "error";
			}
			oldt.setWarehouseTeaquality(total);
			teaxservice.update(oldt);//更新库存信息
			
			//更新日志
			if(teaxlogservice.update(teaxlog)>0){
				return "success";
			}
		}else{//改变了出库原料
			warehouseteax teax=teaxservice.selectbyid(teaxlog.getWarehouseTeaxid());//获取现在库存
			float t=teax.getWarehouseTeaquality()-teaxlog.getWarehouseTeaxquality();
			if(t<0){
				return "error";
			}
			//更新库存(之前，现在的)
			oldt.setWarehouseTeaquality(oldt.getWarehouseTeaquality()+teaxlog1.getWarehouseTeaxquality());
			 teax.setWarehouseTeaquality(t);
			 teaxservice.update(teax);
			 teaxservice.update(oldt);
			//更新加工信息
			 operate op=operateservice.selectbylogid(teaxlog.getWarehouseTeaxlogid());
			 op.setWarehouseTeaxid(teaxlog.getWarehouseTeaxlogid());
			 operateservice.update(op);
			 //更新日志
			 if(teaxlogservice.update(teaxlog)>0){
					return "success";
				}
		}
		return null;
		
	}
	
	/**删除日志
	 * 更新库存
	 * 如果未加工-------删除加工表
	 * 加工------------删除失败
	 * @param id
	 * @return 
	 */ 
	@RequestMapping("delete")
	@ResponseBody
	public String delete(String id){
		warehouseteaxlog teaxlog=teaxlogservice.selectbyid(id);
		operate op=operateservice.selectbylogid(id);
		if(op.getState()!=0){
			return "false";
		}else{
			operateservice.deletebyid(op.getOperateId());
			warehouseteax teax=teaxservice.selectbyid(teaxlog.getWarehouseTeaxid());
			teax.setWarehouseTeaquality(teax.getWarehouseTeaquality()+teaxlog.getWarehouseTeaxquality());
			teaxservice.update(teax);
			teaxlogservice.deletebyid(id);
			return "success";
		}

		
	}
	
	
}
